package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.ArticleMapper;
import com.hualan.sfzy.model.dto.ArticleDto;
import com.hualan.sfzy.model.entity.Article;
import com.hualan.sfzy.model.vo.ArticleQuery;
import com.hualan.sfzy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int add(Article entity) {
        entity.setArticleTime(new Date());
        return articleMapper.insert(entity);
    }

    @Override
    public int edit(Article entity) {
        return articleMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        // 先设置删除时间，不影响创建时间
        articleMapper.updateRecycleTime(id, new Date());
        return articleMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return articleMapper.deleteBatch(ids);
    }

    @Override
    public List<Article> find() {
        return articleMapper.select(new Article());
    }

   @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Article entity) {
           // 可以空实现（如果确认不会被调用），或抛出不支持的异常
           throw new UnsupportedOperationException("此方法不支持，请使用findArticlePage");

    }

    @Override
    public  Map<String, Object> findArticlePage(Integer pageNum, Integer pageSize, ArticleQuery entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleDto> list = articleMapper.select(entity);
        PageInfo<ArticleDto> pageInfo = new PageInfo<>(list);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        pageMap.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return pageMap;
    }
    @Override
    public Article findById(Integer id) {
        return articleMapper.selectById(id);
    }
    
    @Override
    public List<ArticleDto> findAll() {
        return articleMapper.selectAll();
    }

    @Override
    public List<ArticleDto> findAllDelete() {
        return articleMapper.selectAllDelete();
    }
    @Override
    public Map<String, Object> findArticlePageDelelete(Integer pageNum, Integer pageSize, ArticleQuery query) {
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleDto> list = articleMapper.selectDelete(query);
        PageInfo<ArticleDto> pageInfo = new PageInfo<>(list);
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put(CommonConstant.PAGE_LIST, list);
        pageMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        pageMap.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return pageMap;
    }
    @Override
    public int recover(Integer id) {
        return articleMapper.recover(id);
    }
    @Override
    public int recoverBatch(Integer[] ids) {
        return articleMapper.recoverBatch(ids);
    }
    @Override
    public int removecompletely(Integer id){
        return articleMapper.deletecompletely(id);
    }
    @Override
    public int removeBatchCompletely(Integer[] ids){
        return articleMapper.deleteBatchCompletely(ids);
    }
    
    @Override
    public int clearExpiredArticles() {
        return articleMapper.deleteExpiredArticles(15); // 清理超过15天的文章
    }
    
    @Override
    public ArticleDto findRecycleById(Integer id) {
        return articleMapper.selectRecycleById(id);
    }
    @Override
    public int incrementReadCount(Integer id, Integer count) {
        if (id == null || count == null || count <= 0) {
            throw new IllegalArgumentException("参数不合法");
        }

       return articleMapper.updateCount(id, count);
    }
}